package com.goodsogood.union.auth.service;

import java.util.UUID;

import com.goodsogood.union.auth.conf.TokenExpireConfig;

public class AccessToken {
	private String id ;
	private long createTime;
	private long lastAccessTime;
	private long expireIn; 
	private boolean refeshable ;
	private String userId ;
	private String deviceId ;
	
	public String getDeviceId() {
		return deviceId;
	}

	public void setDeviceId(String deviceId) {
		this.deviceId = deviceId;
	}
	private AccessToken(){
		
	}
	public AccessToken(String userId){
		this.id = UUID.randomUUID().toString(); 
		this.userId = userId ; 
		this.lastAccessTime  = this.createTime = System.currentTimeMillis() ;
        this.expireIn = TokenExpireConfig.USER_ACCESS_TOKEN_EXPIRE ;
        this.refeshable = true ; 
	}
	
	public boolean isExpired(){
		return (System.currentTimeMillis() - lastAccessTime ) >  (expireIn * 1000 );
	}
	public String getUserId() {
		return userId;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	}
	public boolean isRefeshable() {
		return refeshable;
	}
	public void setRefeshable(boolean refeshable) {
		this.refeshable = refeshable;
	}
	private String plateformDeviceType ;
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public long getCreateTime() {
		return createTime;
	}
	public void setCreateTime(long createTime) {
		this.createTime = createTime;
	}
	public long getLastAccessTime() {
		return lastAccessTime;
	}
	public void setLastAccessTime(long lastAccessTime) {
		this.lastAccessTime = lastAccessTime;
	}
	
	public long getExpireIn() {
		return expireIn;
	}
	public void setExpireIn(long expireIn) {
		this.expireIn = expireIn;
	}
	public String getPlateformDeviceType() {
		return plateformDeviceType;
	}
	public void setPlateformDeviceType(String plateformDeviceType) {
		this.plateformDeviceType = plateformDeviceType;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		AccessToken other = (AccessToken) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
	
}
